Page({
  data: {
    // 当前日期
    currentYear: 0,
    currentMonth: 0,
    currentDay: 0,
    currentWeekday: 0,
    weekdayMap: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
    
    // 接口原始数据
    apiData: {},
    
    // 处理后的时辰数据
    timePeriods: [],
    filteredPeriods: [], // 用于筛选显示的时辰
    
    // 更新时间
    updateTime: '',
    
    // 分类状态
    activeCategory: 'all'
  },
  
  onLoad() {
    // 初始化日期
    const today = new Date();
    this.setData({
      currentYear: today.getFullYear(),
      currentMonth: today.getMonth() + 1,
      currentDay: today.getDate(),
      currentWeekday: today.getDay(),
      updateTime: this.formatTime(today)
    });
    
    // 加载本地假数据
    this.loadLocalData();
  },
  
  // 格式化时间
  formatTime(date) {
    const hour = date.getHours()
    const minute = date.getMinutes()
    return `${[hour, minute].map(this.formatNumber).join(':')}`
  },
  
  formatNumber(n) {
    n = n.toString()
    return n[1] ? n : `0${n}`
  },
  
  // 加载本地假数据
  loadLocalData() {
    
    // 接口返回的示例数据
    const apiData = {
      "code": 200,
      "ynian": "1900",
      "yyue": "2",
      "yri": "1",
      "zi0": "子",
      "zi1": "凶",
      "zi2": "丙子时 23:00 - 00:59 冲马 煞南",
      "zi3": "喜神西南 财神正西 福神正东",
      "zi4": "结婚|出行|祈福|安葬|求嗣|纳财",
      "zi5": "盖屋|入殓|上梁",
      "chou0": "丑",
      "chou1": "吉",
      "chou2": "丁丑时 01:00 - 02:59 冲羊 煞东",
      "chou3": "喜神正南 财神正西 福神正东",
      "chou4": "结婚|出行|交易|入宅|开业|赴任|祈福|安床|安葬|修造|求嗣|纳财",
      "chou5": "无",
      "yin0": "寅",
      "yin1": "凶",
      "yin2": "戊寅时 03:00 - 04:59 冲猴 煞北",
      "yin3": "喜神东南 财神正北 福神正北",
      "yin4": "结婚|入宅|安葬",
      "yin5": "出行|搬家|赴任|诉讼|祈福|乘船|修造|求嗣",
      "mao0": "卯",
      "mao1": "凶",
      "mao2": "己卯时 05:00 - 06:59 冲鸡 煞西",
      "mao3": "喜神东北 财神正北 福神正南",
      "mao4": "结婚|入宅|开业|祈福|安葬|求嗣|纳财",
      "mao5": "出行|赴任|动土|修造",
      "chen0": "辰",
      "chen1": "吉",
      "chen2": "庚辰时 07:00 - 08:59 冲狗 煞南",
      "chen3": "喜神西北 财神正东 福神西南",
      "chen4": "结婚|出行|搬家|赴任|安葬|作灶|进人口|纳财",
      "chen5": "动土|祈福|祭祀|修造|酬神|斋醮",
      "si0": "巳",
      "si1": "凶",
      "si2": "辛巳时 09:00 - 10:59 冲猪 煞东",
      "si3": "喜神西南 财神正东 福神西南",
      "si4": "结婚|入宅|祈福|盖屋|安葬|修造|作灶|造庙|纳财",
      "si5": "出行|赴任|乘船|造船",
      "wu0": "午",
      "wu1": "吉",
      "wu2": "壬午时 11:00 - 12:59 冲鼠 煞北",
      "wu3": "喜神正南 财神正南 福神西北",
      "wu4": "结婚|搬家|入宅|安床|安葬|修造|求嗣",
      "wu5": "出行|赴任|祈福|祭祀|开光|斋醮",
      "wei0": "未",
      "wei1": "凶",
      "wei2": "癸未时 13:00 - 14:59 冲牛 煞西",
      "wei3": "喜神东南 财神正南 福神正西",
      "wei4": "结婚|搬家|入宅|开业|安床|盖屋|修造|作灶|求嗣",
      "wei5": "出行|赴任|祈福|祭祀|开光|斋醮",
      "shen0": "申",
      "shen1": "凶",
      "shen2": "甲申时 15:00 - 16:59 冲虎 煞南",
      "shen3": "喜神东北 财神东北 福神东南",
      "shen4": "结婚|出行|交易|开业|安床|求嗣|纳财",
      "shen5": "祈福|安葬|祭祀|修造|酬神|开光|斋醮",
      "you0": "酉",
      "you1": "凶",
      "you2": "乙酉时 17:00 - 18:59 冲兔 煞东",
      "you3": "喜神西北 财神西南 福神东南",
      "you4": "结婚|出行|交易|开业|祈福|安床|求嗣|纳财",
      "you5": "诸事不宜",
      "xu0": "戌",
      "xu1": "吉",
      "xu2": "丙戌时 19:00 - 20:59 冲龙 煞北",
      "xu3": "喜神西南 财神正西 福神正东",
      "xu4": "结婚|出行|交易|开业|赴任|祈福|安床|求嗣|纳财",
      "xu5": "盖屋|入殓|上梁",
      "hai0": "亥",
      "hai1": "吉",
      "hai2": "丁亥时 21:00 - 22:59 冲蛇 煞西",
      "hai3": "喜神正南 财神正西 福神正东",
      "hai4": "无",
      "hai5": "诸事不宜"
    };
    
    // 保存原始接口数据
    this.setData({ apiData });
    
    // 转换数据格式
    const periods = ['zi', 'chou', 'yin', 'mao', 'chen', 'si', 'wu', 'wei', 'shen', 'you', 'xu', 'hai'];
    const timePeriods = periods.map(period => {
      // 解析时间段
      const fullName = apiData[`${period}2`] || '';
      const timeRangeMatch = fullName.match(/(\d{2}:\d{2} - \d{2}:\d{2})/);
      const timeRange = timeRangeMatch ? timeRangeMatch[1] : '';
      
      // 解析冲煞信息
      const chongShaMatch = fullName.split(' ').slice(2).join(' ');
      const chongSha = chongShaMatch || '';
      
      // 处理宜事项
      let suitable = [];
      if (apiData[`${period}4`] && apiData[`${period}4`] !== '无') {
        suitable = apiData[`${period}4`].split('|');
      }
      
      // 处理忌事项
      let forbidden = [];
      if (apiData[`${period}5`] && apiData[`${period}5`] !== '无') {
        forbidden = apiData[`${period}5`].split('|');
      }
      
      return {
        period: period,
        name: apiData[`${period}0`] || '',
        goodOrBad: apiData[`${period}1`] || '',
        fullName: fullName,
        timeRange: timeRange,
        chongSha: chongSha,
        godsDirection: apiData[`${period}3`] || '',
        suitable: suitable,
        forbidden: forbidden,
        isExpanded: false
      };
    });
    
    this.setData({ 
      timePeriods,
      filteredPeriods: timePeriods, // 初始显示全部
      activeCategory: 'all'
    });
  },
  
  // 日期导航
  changeDate(dayOffset) {
    const currentDate = new Date(this.data.currentYear, this.data.currentMonth - 1, this.data.currentDay);
    currentDate.setDate(currentDate.getDate() + dayOffset);
    
    this.setData({
      currentYear: currentDate.getFullYear(),
      currentMonth: currentDate.getMonth() + 1,
      currentDay: currentDate.getDate(),
      currentWeekday: currentDate.getDay()
    });
    
    // 实际项目中这里应该重新获取数据
    // this.fetchCalendarData();
  },
  
  prevDay() {
    this.changeDate(-1);
  },
  
  nextDay() {
    this.changeDate(1);
  },
  
  toToday() {
    const today = new Date();
    this.setData({
      currentYear: today.getFullYear(),
      currentMonth: today.getMonth() + 1,
      currentDay: today.getDate(),
      currentWeekday: today.getDay()
    });
  },
  
  // 展开/收缩详情
  toggleDetail(e) {
    const period = e.currentTarget.dataset.period;
    const timePeriods = [...this.data.timePeriods];
    const index = timePeriods.findIndex(item => item.period === period);
    
    if (index !== -1) {
      // 关闭其他已展开的卡片
      timePeriods.forEach((item, i) => {
        if (i !== index) {
          item.isExpanded = false;
        }
      });
      
      // 切换当前卡片状态
      timePeriods[index].isExpanded = !timePeriods[index].isExpanded;
      
      this.setData({ 
        timePeriods,
        filteredPeriods: [...timePeriods]
      });
    }
  },
  
  // 筛选吉时
  filterGood() {
    const filtered = this.data.timePeriods.filter(item => item.goodOrBad === '吉');
    this.setData({ 
      filteredPeriods: filtered,
      activeCategory: 'good' 
    });
  },
  
  // 筛选凶时
  filterBad() {
    const filtered = this.data.timePeriods.filter(item => item.goodOrBad === '凶');
    this.setData({ 
      filteredPeriods: filtered,
      activeCategory: 'bad' 
    });
  },
  
  // 显示全部
  showAll() {
    this.setData({ 
      filteredPeriods: [...this.data.timePeriods],
      activeCategory: 'all' 
    });
  }
});